package cn.com.fuhuitong.main.mine.ui.activity

import android.annotation.SuppressLint
import android.text.Editable
import androidx.lifecycle.Observer
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.main.base.ViewModeActivity
import com.alibaba.android.arouter.facade.annotation.Route
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.entity.MainBean
import cn.com.fuhuitong.entity.WebConfig
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.mine.entity.WithdrawConfigEntity
import cn.com.fuhuitong.main.mine.entity.WithdrawEntity
import cn.com.fuhuitong.main.mine.vm.WithdrawViewModel
import cn.com.fuhuitong.utils.BankUtils
import cn.com.fuhuitong.utils.WebViewUtils
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.jakewharton.rxbinding2.widget.RxTextView
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Function6
import kotlinx.android.synthetic.main.activity_mine_withdraw.*

@Route(path = ARouteConstance.ACTIVITY_URL_MINE_WITHDRAW)
class MineWithdrawActivity : ViewModeActivity<WithdrawViewModel>() {

    private var isWithdrawTotal: Boolean = false

    private val mType: Int
        get() = intent.getIntExtra("actionType", 1)

    override fun viewModel(): WithdrawViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_mine_withdraw

    override fun initView() {

        image_withdraw_back.setOnClickListener {
            this.finish()
        }

        val observableName: Observable<CharSequence> =
            RxTextView.textChanges(edit_withdraw_name)
        val observablePhone: Observable<CharSequence> =
            RxTextView.textChanges(edit_withdraw_phone)
        val observableBankName: Observable<CharSequence> =
            RxTextView.textChanges(edit_withdraw_bank_name)
        val observableBank: Observable<CharSequence> =
            RxTextView.textChanges(edit_withdraw_bank)
        val observableBankSure: Observable<CharSequence> =
            RxTextView.textChanges(edit_withdraw_bank_sure)
        val observableMoney: Observable<CharSequence> =
            RxTextView.textChanges(edit_withdraw_money)


        val observable: Disposable = Observable.combineLatest(
            observableName,
            observablePhone,
            observableBankName,
            observableBank,
            observableBankSure,
            observableMoney,
            Function6<CharSequence, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence, Boolean> { t1, t2, t3, t4, t5, t6 ->
                t1.isNotEmpty() && t2.isNotEmpty() && t3.isNotEmpty() && t4.isNotEmpty() && t5.isNotEmpty() && t6.isNotEmpty()
            })
            .subscribe {
                text_withdraw_submit.isEnabled = it
            }
        viewModel.dispose().add(observable)

        text_withdraw_details.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_MINE_WITHDRAW_LIST)
                .withInt("actionType", mType)
                .navigation()
        }
        text_withdraw_description.setOnClickListener {

//            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_WEB_VIEW)
//                .withSerializable(
//                    AppConstance.BUNDLE_WEB_VIEW,
//                    MainBean(type = WebConfig.WITHDRAW_DESCRIPTION, data = null)
//                )
//                .navigation()
        }

        text_withdraw_total.setOnClickListener {
            isWithdrawTotal = true
            initData()
        }

        text_withdraw_submit.setOnClickListener {
            if (edit_withdraw_bank.text.toString() != edit_withdraw_bank_sure.text.toString()) {
                toast("确认银行卡号错误")
            } else {
                val bankName = BankUtils(edit_withdraw_bank.toString())
                LogUtils.e(bankName)
                val entity = WithdrawEntity(
                    name = edit_withdraw_name.text.toString(),
                    mobile = edit_withdraw_phone.text.toString(),
                    bank_name = edit_withdraw_bank_name.text.toString(),
                    bank_num = edit_withdraw_bank.text.toString(),
                    price = edit_withdraw_money.text.toString()
                )
                if (mType == 1) {
                    viewModel.addStoreCash(entity)
                } else if (mType == 2) {
                    viewModel.addCash(entity)
                }
            }
        }
    }

    override fun initViewMode() {
        viewModel.withdrawConfigDataLiveData.observe(this, Observer { response ->
            if (isWithdrawTotal) {
                handlerResponseLoading(response.status, response.statusTip)
            }
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                this.fillDate(it)
            }
        })

        viewModel.baseDataLiveData.observe(this, Observer { response ->
            handlerResponseLoading(response.status, response.statusTip)
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
        })


        viewModel.configDataLiveData.observe(this, Observer { response ->
            handlerResponseLoading(response.status, response.statusTip)
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                text_withdraw_description.text = "提现说明：${it.cash_rule}"
            }
        })
    }

    override fun initData() {
        if (mType == 1) {
            viewModel.cashStorePrice()
        } else if (mType == 2) {
            viewModel.cashPrice()
        }

        viewModel.configValue(arrayOf("cash_rule"))
    }

    @SuppressLint("SetTextI18n")
    private fun fillDate(configEntity: WithdrawConfigEntity) {
        text_withdraw_price_max.text = "*可提现${configEntity.max_price}"
        if (isWithdrawTotal) {
            edit_withdraw_money.text =
                Editable.Factory.getInstance().newEditable(configEntity.max_price)
        }
        isWithdrawTotal = false
    }

}